<template>
  <view class="launch-main">
    <qxPop ref="qxpop" :obj="obj" :getlaunch="getlaunch"></qxPop>
    <agreement ref="agreement" :update="update"></agreement>
    <view class="with-ad">
      <view class="advertisement">
        <view class="skip" @click="clickHome" v-if="USER_ORIENTED">跳过</view>
        <image :src="adImage" class="advertisement" @load="imageLoaded">
        </image>
      </view>
      <view class="logo-part" @click="clickHome()">
        <image
          class="logo"
          src="/static/bothImages/bbguanjiaLogo.png"
          mode="aspectFit"
        ></image>
        <view class="text-area">
          <text class="title">{{ title }}</text>
        </view>
      </view>
    </view>
    <view class="without-ad"></view>
  </view>
</template>

<script>
import { SHORT_TOAST_DURATION } from '../../util/misc';
import { didntLoggedIn, uniRedirect, uniReLaunch } from '../../util/funcs';
import { uniRequest } from '../../util/network';
import { uniHideLoading } from '../../util/commonFuncs';
import { USER_ORIENTED } from '../../util/config';
import { appMixin } from '../../util/reuse';
import { permission } from '../../util/wa-permission/permission';
import { TESTING } from '../../util/test';
import photo from '../login/protocol.vue'
export default {
  components:{photo},
  mixins: [appMixin],
  data() {
    return {
      adTime: 4 * 1000,
      judging: false,
      clicked: false,
      adLoaded: false,
      noImage: true,
      logoPartStyle: {},
      title: '专注商业 创新超越',
      adImage: '',
      permissionAsked: false,
      USER_ORIENTED: USER_ORIENTED,
      obj:{}
    };
  },
  onLoad: async function () {
    this.judgeSystemPermission();
    //#ifdef APP-PLUS
		// this.update()
	  let system = uni.getSystemInfoSync().platform;
	  if(system == 'ios'){
		  if(uni.getStorageSync('first')){
			this.update()
		  }else{
			this.$nextTick(()=>{
			  this.$refs.agreement.show(true)
			})
		  }
	  }else{
		  this.update()
	  }
    //#endif
    //#ifndef APP-PLUS
      this.getlaunch()
    //#endif
  },
  methods: {
    update(){
      plus.runtime.getProperty(plus.runtime.appid,(wgtinfo)=>{ 
        uniRequest('/check_update_package',{
          version:wgtinfo.version
        },{
          success:res=>{ 
            if(res.data.code==200){
              this.$refs.qxpop.show(true)
              this.obj = res.data.data
            }else{
              this.getlaunch()
            }
          }
        })
      })
    },
    getlaunch(){
      if (USER_ORIENTED) {
        // uniShowLoading('加载中')
        setTimeout(() => {
          if (this.adLoaded === false) {
            this.goHome();
          }
        }, 20000);
        uniRequest(
          '/advert',
          {},
          {
            success: ({ data }) => {
              uniHideLoading();
              this.adImage = data.data[0].design_img;
            },
          }
        );
      } else {
        uniHideLoading();
        setTimeout(() => {
          this.goHome();
        }, SHORT_TOAST_DURATION);
      }
    },
    judgeSystemPermission() {
      // #ifdef APP-PLUS
      // 获取定位权限(通过初始化定位)
      permission.judgeIosPermission('location');
      permission.requestAndroidPermission();
      // #endif
    },
    imageLoaded() {
      this.adLoaded = true;
      console.log('ad loaded');
      setTimeout(() => {
        this.goHome();
      }, this.adTime); // 广告时间
    },
    clickHome() {
      if (!USER_ORIENTED && !TESTING) return;
      this.clicked = true;
      uniReLaunch('/home/home');
    },
    goHome() {
      if (this.clicked) return;
      if (!USER_ORIENTED) {
        if (didntLoggedIn(false, false)) {
          console.warn('招商未登录');
          uniRedirect('/login/login');
        } else {
          setTimeout(() => {
            uniReLaunch('/home/home');
          }, SHORT_TOAST_DURATION-500);
        }
      } else {
        uniReLaunch('/home/home');
      }
    },
  },
};
</script>

<style scoped lang="scss">
@import '../../assets/styles/global';

.launch-main {
  @include columnTop;

  .advertisement {
    height: 75vh;
    width: 100vw;
    background-color: white;
    position: relative;
    @include cover-no-repeat-center;
    .skip {
      position: absolute;
      right: 4vw;
      top: 8vh;
      width: 15vw;
      height: 7vw;
      background-color: rgba(211, 211, 211, 0.5);
      z-index: 1;
      @include center;
      color: white;
      font-size: 28rpx;
      border-radius: 3vw;
      /*border: 1px #717c82 solid;*/
    }
  }

  .logo-part {
    margin-top: 25px;
    @include center;
    flex-direction: column;
    bottom: 100px;

    .title {
      font-size: 28rpx;
      color: #8f8f94;
      font-weight: normal;
    }
  }

  .logo {
    width: 40vw;
    height: 25vw;
  }

  .text-area {
    display: flex;
    justify-content: center;
  }
}
</style>
